<template>
    <div id="report">
        <div class="card-container">
            <el-row :gutter="10">
                <el-form :model="queryParams" label-width="100px" ref="queryForm" :inline="true">
                    <el-col :span="19" :offset="0">
                        <el-form-item label="项目名称" prop="projectId">
                            <el-select v-model="queryParams.projectId" clearable :style="'width:215px'"
                                placeholder="请选择所属项目">
                                <el-option v-for="item in projectList" :key="item.projectId" :label="item.shortName"
                                    :value="item.projectId">
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="运行日期" prop="monthTime">
                            <el-date-picker type="months" value-format="yyyy-MM" v-model="queryParams.monthTime"
                                popper-class="date_form" :picker-options="pickerOptions" placeholder="选择一个或多个月">
                            </el-date-picker>
                            <!--  :clearable="false" -->
                            <el-date-picker type="dates" format="dd" value-format="yyyy-MM-dd" v-model="queryParams.dayTime"
                                popper-class="day_form" :picker-options="pickerOptions" placeholder="选择一个或多个天">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="5" :offset="0">
                        <el-form-item>
                            <el-button icon="el-icon-search" size="mini" @click="handleQuery">统计
                            </el-button>
                            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
                        </el-form-item>
                    </el-col>
                </el-form>
            </el-row>
        </div>

        <div class="card-container">
            <el-row :gutter="10" class="mb8">
                <el-col :span="1.5">
                    <el-button icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>
                </el-col>
            </el-row>
            <el-table v-loading="loading" :data="runRateData.oneList">
                <el-table-column prop="code" align="center" show-overflow-tooltip label="设备序列号" min-width="125">
                </el-table-column>
                <el-table-column prop="address" align="center" show-overflow-tooltip label="设备安装地址" min-width="250" />
                <el-table-column prop="runRate" align="center" show-overflow-tooltip label="运行率≥95%" min-width="80" />
            </el-table>
            <el-table v-loading="loading" :data="runRateData.twoList">
                <el-table-column prop="code" align="center" show-overflow-tooltip label="设备序列号" min-width="125">
                </el-table-column>
                <el-table-column prop="address" align="center" show-overflow-tooltip label="设备安装地址" min-width="250" />
                <el-table-column prop="runRate" align="center" show-overflow-tooltip label="95%≥运行率≥90%" min-width="80" />
            </el-table>
            <el-table v-loading="loading" :data="runRateData.threeList">
                <el-table-column prop="code" align="center" show-overflow-tooltip label="设备序列号" min-width="125">
                </el-table-column>
                <el-table-column prop="address" align="center" show-overflow-tooltip label="设备安装地址" min-width="250" />
                <el-table-column prop="runRate" align="center" show-overflow-tooltip label="运行率≤90%" min-width="80" />
            </el-table>
        </div>
    </div>
</template>

<script>
import { getrunLogStatistics, exportRunLog } from '@/api/runStatistics/index.js'
import { getProject } from "@/api/runningLog/runningLog.js";
import moment from 'moment';
export default {
    dicts: [
        "sys_device_mode"
    ],
    data() {
        return {
            runRateData: {},
            queryParams: { projectId: null, monthTime: [], monthList: null, dayTime: [], dayList: null },
            projectList: [],
            runRateList: [],
            total: 0,
            tableList: [],
            cueerntPage: 1,
            pageSize: 10,
            loading: false,
            exceptionData: [],
            pickerOptions: {
                //禁用当前日期之前的日期
                disabledDate(time) {
                    //Date.now()是javascript中的内置函数，它返回自1970年1月1日00:00:00 UTC以来经过的毫秒数。
                    return time.getTime() > Date.now();
                },
            },
        }
    },
    created() {
        this.getConfig()
        let nowMonth = moment().startOf('month').format('YYYY-MM')
        this.queryParams.monthTime.push(nowMonth)
        this.queryParams.monthList = nowMonth
        this.getList()
    },
    methods: {
        async getConfig() {
            const res = await getProject();
            if (res.code == 200) {
                this.projectList = res.data;
            }
        },

        // 获取列表数据
        async getList() {
            this.loading = true
            if (this.queryParams.dayTime?.length != 0 || this.queryParams.dayTime) {
                this.queryParams.dayList = this.queryParams.dayTime?.join()
            }
            let res = await getrunLogStatistics(this.queryParams)
            this.loading = false

            if (res.code == 200) {
                // 倒序排列
                for (const key in res.data) {
                    res.data[key] = res.data[key].sort(function (a, b) { return parseFloat(b.runRate) - parseFloat(a.runRate) })
                }
                this.runRateData = res.data
            }
        },


        handleQuery() {
            this.cueerntPage = 1;
            this.pageSize = 10
            this.queryParams.monthList = this.queryParams.monthTime?.join()
            this.queryParams.dayList = this.queryParams.dayTime?.join()
            if ((this.queryParams.monthTime && this.queryParams.monthTime?.length > 0) || (this.queryParams.dayTime && this.queryParams.dayTime?.length > 0)) {
                this.getList()
            } else {
                this.$modal.msgError('运行日期不能为空')
            }
        },
        resetQuery() {
            this.queryParams.monthList = moment().startOf('month').format('YYYY-MM')
            // 获取当前月份的天数
            // const daysInMonth = currentDate.daysInMonth();
            this.queryParams.dayTime = []
            // 构建日期列表
            this.resetForm("queryForm");
            this.handleQuery();
        },
        async handleExport() {
            this.download(
                "runLogStatistics/export",
                {
                    ...this.queryParams,
                },
                `runLog_${new Date().getTime()}.xlsx`
            );
        }
    }
}
</script>
<style lang='scss' scoped>
#report {
    width: 100%;
    height: 100%;
    padding: 20px;
    position: relative;
    box-sizing: border-box;
}

.title {
    font-size: 16px;
    font-weight: 600;
}

.time-box {
    margin-top: 10px;
    display: flex;
    flex-direction: column;

    >span {
        margin-bottom: 10px;
    }

}

::v-deep .el-date-editor--dates {
    margin-left: 20px;
}
</style>
<style >
.day_form .el-icon-d-arrow-left,
.el-icon-d-arrow-right {
    display: none;
}

.day_form .el-date-picker__header-label:first-of-type {
    display: none;
}

.day_form .el-date-picker__header-label,
.date_form .el-date-picker__header-label {
    color: #fff;
}

.day_form .el-picker-panel__icon-btn {
    color: #fff;
}

.day_form .el-date-picker__header {
    padding: 6px;

}

.date_form .el-month-table td .cell {
    color: #fff;
}

.date_form .el-month-table td.disabled .cell {
    color: #C0C4CC;
}
</style>